<?php
namespace app\www\controller;
class Tool extends Base{

    //抓取商品分类
    public function get_goods_cat()
    {
        //抓取一级分类
        $rs = $this->db1->query('select * from goodstypeone where is_count=0', [], 1);
        if (!$rs) t('end');
        $data = [
            "parent_id" => 0,
            "cat_title" => $rs["name"],
            "sub_title" => $rs["name"],
            "cat_tree" => ",0,",
            "sort" => $rs["list"],
            "level" => 1,
            "create_time" => time(),
        ];
        $d = $this->db->insert("goods_cat", $data);
        //
        if ($rs) {
            $data = [
                'old_id' => $d,
                'is_count' => 1,
            ];
            $where = [
                'id=?',
                [$rs['id']]
            ];
            $this->db1->update('goodstypeone', $data, $where);
        }
        //抓取二级分类
        $goods_cat_two = $this->db1->query('select * from goodstypetwo where is_count=0 and typeOneId = ?', [$rs['id']]);
        if ($goods_cat_two) {
            foreach ($goods_cat_two as $k => $v) {
                $goods_cat_two_data = [
                    "parent_id" => $d,
                    "cat_title" => $v["name"],
                    "sub_title" => $v["name"],
                    "cat_tree" => ",0," . $d . ",",
                    "sort" => $v["list"],
                    "level" => 2,
                    "create_time" => time(),
                ];
                $d_1 = $this->db->insert("goods_cat", $goods_cat_two_data);
                if ($d) {
                    $data = [
                        'old_id' => $d_1,
                        'is_count' => 1,
                    ];
                    $where = [
                        'id=?',
                        [$v['id']]
                    ];
                    $this->db1->update('goodstypetwo', $data, $where);
                }
            }
        }
        e(date('H:i:s'));
        js_url('', 0.01);
    }

    public function get_goods_cat_3()
    {
        $rs = $this->db1->query('select * from goodstypetwo where is_count=0', [], 1);
        if (!$rs) t('end');
        $cat_info = $this->db->find("goods_cat", $rs['old_id']);
        $goods_cat_three = $this->db1->query('select * from goodstypethree where is_count=0 and typeTwoId = ?', [$rs['id']]);
        if ($goods_cat_three) {
            foreach ($goods_cat_three as $k => $v) {
                $goods_cat_two_data = [
                    "parent_id" => $cat_info['id'],
                    "cat_title" => $v["name"],
                    "sub_title" => $v["name"],
                    "cat_tree" => $cat_info['cat_tree'] . $cat_info['id'] . ",",
                    "sort" => $v["list"],
                    "level" => 3,
                    "create_time" => time(),
                ];
                $d_1 = $this->db->insert("goods_cat", $goods_cat_two_data);
                if ($d_1) {
                    $data = [
                        'is_count' => 1,
                    ];
                    $where = [
                        'id=?',
                        [$v['typeTwoId']]
                    ];
                    $this->db1->update('goodstypetwo', $data, $where);
                    $data["old_id"] = $d_1;
                    $where = [
                        'id=?',
                        [$v['id']]
                    ];
                    $this->db1->update('goodstypethree', $data, $where);
                }
            }
        }
        e(date('H:i:s'));
        js_url('', 0.01);
    }

    public function getGoods()
    {
        $old_goods = $this->db1->query('select * from goods where is_count=0', [], 1);
        if (!$old_goods)
            t('end');

        $cat = $this->db1->query('select * from goodstypethree where id =?', [$old_goods['typeThreeId']], 1);
        $img = $this->db1->query('select * from goodswin where goodsid = ?', [$old_goods['id']]);
        $img1 = array_column($img, 'img');
        $pics = join(",", $img1);
        $img = $this->db1->query('select * from article where targetId = ?', [$old_goods['id']]);
        $img = array_column($img, 'img');
        $content = "";
        foreach ($img as $v) {
            $content .= "<img src='{$v}'/>";
        }
        //获取规格
        $goods_sku = $this->db1->query('select * from goodssku where goodsId = ?',[$old_goods['id']]);
        $spec = [];
        if($goods_sku){
            $spec = [];
            foreach ($goods_sku as $v){
                $spec['规格'][] = $v['name'];
            }
        }else{
            $spec['规格'][] = $old_goods['name'];
        }
        $spec = json_encode($spec);
        $data = [
            'shop_id' => 1,
            'cat_id' => $cat['old_id'],
            'title' => $old_goods['name'],
            'keyword' => $old_goods['name'],
            'description' => $old_goods['summary'],
            'price' => $old_goods['price'],
            'market_price' => $old_goods['marketPrice'],
            'pic' => $old_goods['ico'],
            'views' => $old_goods['clickRate'],
            'sales_volume' => $old_goods['salesVolume'],
            'pics' => $pics,
            'content' => $content,
            'spec' => $spec,
            'create_time' => time(),
        ];
        $id = $this->db->insert("goods", $data);
        //规格添加
        if($goods_sku){
            foreach ($goods_sku as $v){
                $attr_data = [
                    'shop_id' => 1,
                    'goods_id' => $id,
                    'title' => $v['name'],
                    'price' => $v['price'],
                    'stock' => $v['stock'],
                    'sales_volume' => $v['salesVolume'],
                    "create_time" => time()
                ];
                $this->db->insert("goods_price", $attr_data);
            }
        }else{
            $attr_data = [
                'shop_id' => 1,
                'goods_id' => $id,
                'title' => $old_goods['name'],
                'price' => $old_goods['price'],
                'stock' => 100,
                "create_time" => time()
            ];
            $this->db->insert("goods_price", $attr_data);
        }
        #,new_id
        $this->db1->update('goods', ['old_id' => $id, "is_count" => 1], ['id=?', [$old_goods['id']]]);
        e(date('H:i:s'));
        js_url('', 0.01);
    }

    //抓取商品图片
    public function getpic()
    {
        $rs = $this->db->query('select * from goods where is_count=0', [], 1);
        if (!$rs) t('end');
        $pic = [];
        $pic[] = $rs['pic'];
        $pic = array_merge($pic, array_filter(explode(',', $rs['pics'])));
        $content = $rs["content"];
        $pattern = "/<img.+src=\"?(.+.(jpg|gif|bmp|bnp|png))\"?.+>/i";
        preg_match_all("/&lt;img src='(.*?)'&gt;/", $content, $match);
        $pic = array_merge($pic, $match[1]);
        #正则 图片地址 $pic
        #采集
        foreach ($pic as $v) {
            $pic_url = str_replace("/uploads", "http://shop.ydyigo.com", $v);
            $content = file_get_contents($pic_url);
            com_mkdir(dirname(APP_PATH . '../public' . $v));
            file_put_contents("." . $v, $content);
            #存入数据库
            $data = [
                'table' => 'goods',
                'table_id' => $rs['id'],
                'status' => 1,
                'url' => $v
            ];
            $this->db->insert('attachment', $data);
        }

        #更新is_count
        $data = [
            'id' => $rs['id'],
            'is_count' => 1,
        ];
        $this->db->update('goods', $data);
        e(date('H:i:s'));
        js_url('', 0.01);
    }
    ##sql语句处理图片路径
    ##update goods set pic=REPLACE(pic,"img/GoodsIco/","/uploads/img/GoodsIco/");
    ##update goods set pics=REPLACE(pics,"img/GoodsWin/","/uploads/img/GoodsWin/");
    ##update goods set content=REPLACE(content,"img/ArticleImg/","/uploads/img/ArticleImg/");
    /**
     * \对没有规格的商品进行处理
     */
    public function goodsPriceExcute(){
        $rs = $this->db->query('select * from goods where is_count=0', [], 1);
        if(!$rs)t("end");
        $goods_price_info = $this->db->query('select id from goods_price where goods_id=? order by id asc',[$rs['id']],1);
        if(!$goods_price_info){
            $data = [
                'shop_id' => $rs['shop_id'],
                'goods_id' => $rs['id'],
                'title' => $rs['title'],
                'price' => $rs['price'],
                'stock' => 100,
                "create_time" => time()
            ];
            $this->db->insert("goods_price", $data);
        }
        $data = [
            'id' => $rs['id'],
            'is_count' => 1,
        ];
        $this->db->update('goods', $data);
        e(date('H:i:s'));
        js_url('', 0.01);
    }

    //图片处理
    public function goodsPicExcute(){
        $rs = $this->db->query('select * from goods where is_count=0', [], 1);
        if(!$rs)t("end");
        $pic = [];
        $pic[] = $rs['pic'];
        $pic = array_merge($pic, array_filter(explode(',', $rs['pics'])));
        $content = $rs["content"];
        $pattern = "/<img.+src=\"?(.+.(jpg|gif|bmp|bnp|png))\"?.+>/i";
        preg_match_all("/&lt;img src='(.*?)'&gt;/", $content, $match);
        $pic = array_merge($pic, $match[1]);

        var_dump($match);
        die();
    }
}
?>
